//index.js
//获取应用实例
const app = getApp()
import { getOrderList ,topay,refund} from '../../http/api.js';
Page({
  data: {
    current:1,
    paymentStatus:0,
    statusArray:[
      {name:'全部',value:0},
      {name:'未付款',value:1},
      {name:'已付款',value:2},
      {name:'未发货',value:3},
      {name:'已发货',value:4},
      {name:'交易成功',value:5},
      {name:'交易关闭',value:6},
      {name:'待评价',value:7},
      {name:'退款中',value:8},
      {name:'已退款',value:9},
    ],
    orderList:[]
  },
  onShow: function () {
    this.getOrderList();
  },
  //拉取订单列表
  getOrderList:function(e){
    let paymentStatus = this.data.paymentStatus;
    let obj ={
      current: this.data.current,
      size: 10
    }
    if(paymentStatus != 0){
      obj['paymentStatus'] = paymentStatus;
    }
    getOrderList(obj).then(res=>{
      if(res.code == 200){
        if(res.rows && res.rows.length > 0){
          let orderList = this.data.orderList.concat(res.rows);
          this.setData({orderList})
        }
      }
    })
  },
   //下拉刷新
   onPullDownRefresh: function () {
    this.getOrderList();
    wx.stopPullDownRefresh();
  },
  //上拉加载
  onReachBottom: function(){
    let current = this.data.current + 1;
    this.setData({
      current
    });
    this.getOrderList();
  },
  //切换状态
  tabStatus: function(e){
    let paymentStatus = e.currentTarget.dataset.index;
    this.setData({paymentStatus,current:1,orderList:[]});
    this.getOrderList();
  },
    //获取支付参数
    toWxPay: function(e){
      let orderId =  e.currentTarget.dataset.id;
      let vm = this;
      topay(orderId).then(res=>{
        if (res.code == '200') {
          let json = res.data;
          wx.requestPayment({
            timeStamp: json.timeStamp,
            nonceStr: json.nonceStr,
            package: json.package,
            signType: json.signType,
            paySign: json.sign,
            success (res) {
              vm.setData({orderList:[]});
              vm.getOrderList();
            },
            fail (res) {
              
            }
          });
        }
      });
    },
    //申请退款操作
    toRefund: function(e){
      let vm = this;
      wx.showModal({
        title: '友情提示',
        content: '您确定要申请退款吗？',
        success: function(res) {
          if (res.confirm) {
            let orderId =  e.currentTarget.dataset.id;
            refund(orderId).then(res=>{
              if (res.code == '200') {
                vm.setData({orderList:[]});
                vm.getOrderList();
              }
            });
          } 
        }
       })
    }
})
